<template>
  <div class="Container">
    <div class="option">
      <!-- <el-button type="success" size="small" @click="addCarrier()">新增</el-button> -->
      <el-button
        type="primary"
        size="small"
        @click="Refresh()"
      >刷新</el-button>
    </div>
    <el-table
      :data="List"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="index"
        label="序号"
      ></el-table-column>
      <el-table-column
        prop="content"
        label="提示文字"
      ></el-table-column>
      <el-table-column
        prop="math"
        label="兑换比例"
      ></el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img
            :src="`http://www.jfqiandai.com/file${scope.row.log}`"
            style="width:80px"
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="运营商名称"
      ></el-table-column>
      <el-table-column
        prop="num"
        label="顺序"
      ></el-table-column>
      <el-table-column label="二维码图片">
        <template slot-scope="scope">
          <img
            :src="`http://www.jfqiandai.com/file${scope.row.pic}`"
            style="width:80px"
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="type1"
        label="类型"
      ></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="EditClick(scope.row)"
            type="primary"
            plain
            size="small"
          >编辑</el-button>
          <!-- <el-button @click="DeleteClick(scope.row)" type="danger" size="small">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="paginat">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage4"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 弹框 -->
    <el-dialog
      title="提示"
      @close="Cancel"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div class="row">
        <span class="rowTil">提示文字:</span>
        <el-input
          v-model="content"
          placeholder="请输入提示文字"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">兑换比例(积分*比例):</span>
        <el-input
          v-model="math"
          placeholder="请输入兑换比例"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">图片:</span>
        <el-upload
          list-type="picture-card"
          action="http://www.jfqiandai.com/img/upload"
          :file-list="fileList1"
          :on-success="onSuccessLog"
          :on-remove="handleRemoveLog"
          :limit="1"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </div>

      <div class="row">
        <span class="rowTil">运行商名称:</span>
        <el-input
          v-model="name"
          placeholder="请输入运营商名称"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">顺序:</span>
        <el-input
          v-model="num"
          placeholder="请输入顺序"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">二维码图片:</span>
        <el-upload
          list-type="picture-card"
          action="http://www.jfqiandai.com/img/upload"
          :file-list="fileList2"
          :on-success="onSuccessPic"
          :on-remove="handleRemovePic"
          :limit="1"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </div>
      <div class="row">
        <span class="rowTil">类型:</span>
        <el-radio
          v-model="type"
          label="0"
        >银行卡</el-radio>
        <el-radio
          v-model="type"
          label="1"
        >手机卡</el-radio>
      </div>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @close="Cancel">取 消</el-button>
        <el-button
          type="primary"
          @click="isOkClick()"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      List: [],
      total: 0,
      val: 1, //当前多少页
      currentPage4: 1,
      dialogVisible: false,
      fileList1: [],
      fileList2: [],
      flag: 0,
      editId: "",
      content: "", //提示文字
      math: "", //兑换比例
      log: "", // 图片
      name: "", //运行商名称
      num: "", //顺序
      pic: "", //二维码图片
      type: "" //类型
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //图片上传
    onSuccessLog(Response, file, fileList) {
      this.log = file.response.data.path;
    },
    onSuccessPic(Response, file, fileList) {
      this.pic = file.response.data.path;
    },
    handleRemoveLog(file, fileList) {
      this.log = "";
      this.fileList1 = [];
    },
    handleRemovePic(file, fileList) {
      this.pic = "";
      this.fileList2 = [];
    },
    //分页
    handleCurrentChange(val) {
      this.val = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
    //新增
    addCarrier() {
      this.dialogVisible = true;
      this.flag = 1;
      this.content = ""; //提示文字
      this.math = ""; //兑换比例
      this.log = ""; // 图片
      this.name = ""; //运行商名称
      this.num = ""; //顺序
      this.pic = ""; //二维码图片
      this.type = ""; //类型
      this.fileList1 = [];
      this.fileList2 = [];
    },

    //编辑
    EditClick(row) {
      this.dialogVisible = true;
      this.flag = 2;
      this.editId = row.id;
      this.content = row.content; //提示文字
      this.math = row.math;
      this.name = row.name; //运行商名称
      this.num = row.num; //顺序
      this.type = String(row.type); //类型
      this.log = row.log;
      this.pic = row.pic;
      if (this.log) {
        this.fileList1.push({ url: "http://www.jfqiandai.com/file" + this.log });
      } else {
        this.fileList1 = [];
      }
      if (this.pic) {
        this.fileList2.push({ url: "http://www.jfqiandai.com/file" + this.pic });
      } else {
        this.fileList2 = [];
      }
      // this.fileList1[0]["url"] = "http://www.jfqiandai.com/file" + row.log;
      // this.fileList2[0]["url"] = "http://www.jfqiandai.com/file" + row.pic;
    },
          //取消
    Cancel() {
      this.dialogVisible = false;
      this.fileList1 = [];
      this.fileList2 = [];
    },
    //确定
    isOkClick() {
      if (this.flag == 1) {
        let url = "/carrier/add";
        let params = {
          content: this.content, //提示文字
          math: this.math,
          log: this.log, // 图片
          name: this.name, //运行商名称
          num: this.num, //顺序
          pic: this.pic, //二维码图片
          type: this.type //类型
        };
        this.httpPost(url, params, res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.getList();
          }
        });
      } else {
        let url = "/carrier/edit";
        let params = {
          id: this.editId,
          content: this.content, //提示文字
          math: this.math,
          log: this.log, // 图片
          name: this.name, //运行商名称
          num: this.num, //顺序
          pic: this.pic, //二维码图片
          type: this.type //类型
        };
        this.httpPut(url, params, res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.getList();
          }
        });
      }
    },

    //删除
    DeleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let url = "/carrier/edit";
          let params = {
            id: row.id,
            status: 1
          };
          this.httpPut(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //刷新
    Refresh() {
      this.getList();
    },

    //获取数据
    getList() {
      let url = "/carrier/findList";
      let params = {
        pageNum: "",
        pageSize: ""
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.List = res.data.list;
          this.total = res.data.total;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
            this.List[i].type1 = this.List[i].type === 0 ? "银行卡" : "手机卡";
          });
        } else {
          console.log("查询失败");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.Container {
  .option {
    padding: 10px;
  }
  .row {
    padding: 10px;
    .rowTil {
      display: block;
      padding: 10px 0;
    }
  }
}
</style>
